package com.chushouya.product.service.admin.impl;


import com.chushouya.common.support.Contexts;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Dates;
import com.chushouya.product.dao.entity.CategoryEntity;
import com.chushouya.product.dao.repository.CategoryRepository;
import com.chushouya.product.dto.admin.category.CategoryDetailDTO;
import com.chushouya.product.dto.admin.category.CategoryListDTO;
import com.chushouya.product.dto.admin.category.CategoryQuery;
import com.chushouya.product.service.admin.CategoryBrandService;
import com.chushouya.product.service.admin.CategoryService;
import com.chushouya.product.service.admin.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryRepository categoryRepository;

    @Resource
    private ProductService productService;

    @Resource
    private CategoryBrandService categoryBrandService;

    @Override
    public List<CategoryListDTO> selectCategoryList(CategoryQuery query) {
        List<CategoryEntity> categoryEntityList = categoryRepository.selectList(query);
        final List<CategoryListDTO> categoryList = Beans.copyList(categoryEntityList, CategoryListDTO.class);
        return categoryList;
    }

    @Override
    public CategoryDetailDTO getCategoryDetail(Long categoryId) {
        Check.notNull(categoryId, "categoryId can not be null");
        return Beans.copy(categoryRepository.selectByPrimaryKey(categoryId), CategoryDetailDTO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addCategory(CategoryDetailDTO category) {
        CategoryEntity categoryEntity = Beans.copy(category, CategoryEntity.class);
//        categoryEntity.setInitial(PinYinUtil.getFirstIndexSpellUpperCase(category.getCategoryName()));
        if (categoryEntity.getStatus() == null) {
            categoryEntity.setStatus(StatusEnum.normal.value());
        }
        categoryEntity.setCreateTime(Dates.getTimeNow());
        categoryEntity.setUpdateTime(Dates.getTimeNow());
        categoryEntity.setHandleUser(Contexts.getUserContext().getUserName());
        categoryRepository.insertSelective(categoryEntity);
        return categoryEntity.getCategoryId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long editCategory(CategoryDetailDTO category) {
        Check.notNull(category.getCategoryId(), "categoryId can not be null");
        Check.notEmpty(category.getCategoryName(), "分类名称不能为空");
        final CategoryEntity categoryEntity = categoryRepository.selectByPrimaryKey(category.getCategoryId());
        Check.notNull(categoryEntity, "分类不存在");
        CategoryEntity updateEntity = Beans.copy(category, CategoryEntity.class);
//        updateEntity.setInitial(PinYinUtil.getFirstIndexSpellUpperCase(category.getCategoryName()));
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());

        categoryRepository.updateByPrimaryKeySelective(updateEntity);

        return updateEntity.getCategoryId();
    }

    @Override
    public void removeCategory(Long categoryId) {
        Check.notNull(categoryId, "categoryId can not be null");
        Check.notTrue(productService.hasProductBindCategory(categoryId), "有商品关联当前分类，不能删除");
        Check.notTrue(categoryBrandService.hasBrandBindCategory(categoryId), "有品牌关联当前分类，不能删除");
//        Check.notTrue(categoryAttrService.hasCategoryAttrBindCategory(categoryId), "有分类属性关联当前分类，不能删除");
        categoryRepository.deleteByPrimaryKey(categoryId);
    }

    @Override
    public Long editCategoryStatus(Long categoryId, Integer status) {
        Check.notNull(categoryId, "categoryId can not be null");
        Check.notNull(status, "status can not be null");
        CategoryEntity updateEntity = new CategoryEntity();
        updateEntity.setCategoryId(categoryId);
        updateEntity.setStatus(status);
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());
        categoryRepository.updateByPrimaryKeySelective(updateEntity);
        return updateEntity.getCategoryId();
    }

    @Override
    public String getCategoryName(Long categoryId) {
        return categoryRepository.selectByPrimaryKey(categoryId).getCategoryName();
    }

}
